<?
require_once (__DIR__ . '/../models/Accounts.php');
require_once (__DIR__ . '/../models/Products.php');
/**
 * SESSION['cart'] data format:
 * Associative Array of items (by id){
 *  - id
 *  - name
 *  - count
 * }
 * JSON format for:
 *  - GET request (no Id specified):
 * 		cart[]{id,count}
 * 		status = success | failure
 * 		reason (only set when status is failure)
 *  - POST request (or GET request with specific Id):
 * 		item{id,count}
 * 		status = success | failure 
 * 		reason (only set when status is failure)
 * 
 * reasons:
 * 	- 'empty' - cart is empty
 *  - 'POST missing param' - caller did not specify 1 or more required parameters
 *  - 'POST bad id' - caller passed an id that is not valid
 *  - 'POST bad count' - caller passed a count that is not valid
 *  - 'POST bad _request' - caller passed a _request that is not valid
 *  - 'GET unknown request' - bad parameters
 *  - 'did not recognize request' - request type was not GET or POST or PUT or DELETE
 *  - 'no user logged in' - $_SESSION['User'] empty
 *  - '' - unknown error - probably a code bug
 */
if(!array_key_exists('cart', $_SESSION))
	$_SESSION['cart'] = array();

$cart = $_SESSION['cart'];

$requestMethod = !empty($_REQUEST['_method']) ? $_REQUEST['_method'] : $_SERVER['REQUEST_METHOD'];
$data = Array('status'=>'failure','reason'=>''); // assume failure at beginning
if (IsLoggedIn()) {
	switch ($requestMethod) {
		case 'POST': // this is an add to cart request
			if (array_key_exists('_request', $_REQUEST)) // this is a remove or edit request
			{
				if($_REQUEST['_request'] == 'remove'
					&& array_key_exists('id', $_REQUEST) && !empty($_REQUEST['id'])
					&& isset($cart[$_REQUEST['id']]))
				{
					$data = Array( 'item' => $cart[$_REQUEST['id']], 'status' => 'success', 'reason' => '' );
					unset($cart[$_REQUEST['id']]['id']);
					unset($cart[$_REQUEST['id']]['count']);
					unset($cart[$_REQUEST['id']]);
				}
				else if ($_REQUEST['_request'] == 'edit'
					&& array_key_exists('id', $_REQUEST) && !empty($_REQUEST['id'])
					&& array_key_exists('count', $_REQUEST) && !empty($_REQUEST['count'])
					&& isset($cart[$_REQUEST['id']]))
				{
					if ($_REQUEST['count'] > 0)
					{
						$cart[$_REQUEST['id']]['count']=$_REQUEST['count'];	
						$data = Array( 'item' => $cart[$_REQUEST['id']], 'status' => 'success', 'reason' => '' );		
					}else {
						$data = Array( 'item' => null, 'status' => 'failure', 'reason' => 'POST bad count' );		
					}
				}
				else {
					$data = Array( 'item' => null, 'status' => 'failure', 'reason' => 'POST bad _request' );			
				}	
			}
			else if (!empty($_REQUEST['id']) 
				&& array_key_exists('count', $_REQUEST))  // this is an add request...we good?
			{ 
				if (products::Exists($_REQUEST['id']) && $_REQUEST['count'] > 0) // make sure params are valid
				{
					if(!empty($cart[$_REQUEST['id']]))
					{	
						$cart[$_REQUEST['id']]['count'] += $_REQUEST['count'];	
					}
					else{
						$row = products::Get($_REQUEST['id']);
						$cart[$_REQUEST['id']] = Array( 'id' => $_REQUEST['id'], 'count' => $_REQUEST['count']);
						$cart[$_REQUEST['id']]['name'] = $row['name'];				
					}
					// package the updated cart data
					$data = Array( 'item' => $cart[$_REQUEST['id']], 'status' => 'success', 'reason' => '' );												
				}
				else // invalid params
				{
					// package failure message
					if (!products::Exists($_REQUEST['id']))
						$data = Array( 'item' => null, 'status' => 'failure', 'reason' => 'POST bad id' );
					elseif ($_REQUEST['count'] <= 0)
						$data = Array( 'item' => null, 'status' => 'failure', 'reason' => 'POST bad count' );
					else;
				}		
			}
			else // missing required parameters
			{
				// package failure message
				$data = Array( 'item' => null, 'status' => 'failure', 'reason' => 'POST missing param' );									
			}
			
			echo json_encode($data); // send the data 
			break;
		case 'PUT':
			break;
		case 'DELETE':
			break;
		case 'GET': // this is a retrieve cart request
			if (empty($cart)) 
			{
				$data=Array('cart'=>null, 'status'=>'failure', 'reason'=>'empty');
			}
			elseif(empty($_REQUEST['id'])) // if this is a get entire cart request
			{
				$data=Array('cart'=>$cart, 'status'=>'success', 'reason'=>'');
							
			}elseif(!empty($_REQUEST['id'])){ // if this is a specific get request
				$data=Array('item'=>$cart[$_REQUEST['id']], 'status'=>'success', 'reason'=>'');								
			}
			else{
				$data=Array('status'=>'failure', 'reason'=>'GET unknown request');								
			}
			echo json_encode($data);	
			break;
		default: 
			$data=Array('status'=>'failure', 'reason'=>'did not recognize request');
			echo json_encode($data);	
			break;	
	}
}
else{
	$data=Array('status'=>'failure', 'reason'=>'no user logged in');
	echo json_encode($data);	
}
if ($data['status']=='success') // we still good?
	$_SESSION['cart'] = $cart; // update the cart
	